package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.cache.service.IUserInfoRedisService;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.exception.LogicException;
import cn.wolfcode.wolf2w.repository.UserInfoRepository;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.AssertUtil;
import cn.wolfcode.wolf2w.util.Consts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
//@Transactional mongodb事务必须是复制集方式
public class UserInfoServiceImpl implements IUserInfoService{


    @Autowired
    private UserInfoRepository repository;

    @Autowired
    private IUserInfoRedisService userInfoRedisService;

    @Override
    public void save(UserInfo userInfo) {
        userInfo.setId(null);
        repository.save(userInfo);
    }

    @Override
    public void update(UserInfo userInfo) {
        repository.save(userInfo);
    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }

    @Override
    public UserInfo get(String id) {
        return repository.findById(id).orElse(null);
    }

    @Override
    public List<UserInfo> list() {
        return repository.findAll();
    }

    @Override
    public boolean checkPhone(String phone) {

        UserInfo userInfo = repository.findByPhone(phone);

        return userInfo != null;
    }

    @Override
    public void sendVerifyCode(String phone, String smsUrl, String appkey) {

        //1.创建验证码,编辑短信
        String code = UUID.randomUUID().toString().replaceAll("-","").substring(0,4);
        StringBuilder sb = new StringBuilder(80);
        sb.append("您注册验证码是").append(code).append(",请在").append(Consts.VERIFY_CODE_VAI_TIME).append("分钟之内使用");

        //2.发送短信 : 怎么让java发送http请求
        System.out.println(sb.toString());

        /*//Spring提供http请求发送的类
        RestTemplate restTemplate = new RestTemplate();
        //参数1:请求url[url中有参数的占位符] 参数2:返回值类型,参数3:请求需要参数
        String ret = restTemplate.getForObject(smsUrl, String.class,phone,"【叩丁狼】"+sb.toString(),appkey);
        System.out.println(ret);
        if(!ret.contains("Success")){
            throw new LogicException("短信发送失败");
        }*/

        //3.将验证码缓存到redis中
        userInfoRedisService.setVerifyCode(phone,code);
    }

    @Override
    public void regist(String phone, String nickname, String password, String rpassword, String verifyCode) {

        //1.判断传入参数是否为null
        AssertUtil.hasLength(phone,"手机号码不能为空");
        AssertUtil.hasLength(nickname,"昵称不能为空");
        AssertUtil.hasLength(password,"手密码不能为空");
        AssertUtil.hasLength(rpassword,"确认密码不能为空");
        AssertUtil.hasLength(verifyCode,"验证码不能为空");

        //2.判断2次输入的密码是否一致
        AssertUtil.isEquals(password,rpassword,"两次输入的密码不一致");
        //3.判断手机号是否符合格式
        Pattern p = Pattern.compile("1\\d{10}");
        Matcher matcher = p.matcher(phone);
        boolean b = matcher.matches();
        if(b == false){
            throw new LogicException("手机号码格式不正确,请重新输入");
        }
        //4.判断手机号码是否唯一
        if(this.checkPhone(phone)){
            throw new LogicException("该手机号码已经被注册了");
        }
        //5.判断验证码是否正确
        String code = userInfoRedisService.getVerifyCode(phone);
        if(code == null || !code.equalsIgnoreCase(verifyCode)){
            throw new LogicException("验证码失效或者输入错误");
        }
        //6.注册
        UserInfo userInfo = new UserInfo();

        userInfo.setNickname(nickname);
        userInfo.setPhone(phone);
        userInfo.setEmail("");
        userInfo.setPassword(password);
        userInfo.setLevel(1);
        userInfo.setCity("");
        userInfo.setHeadImgUrl("/images/default.jpg");
        userInfo.setInfo("");
        userInfo.setState(UserInfo.STATE_NORMAL);



        /*throw new RuntimeException("xxxxxxxxxxxxxxxx 恶心的系统bug xxxxxxxxxxxxxxxx");*/


        this.save(userInfo);
    }

    @Override
    public UserInfo login(String username, String password) {
        //判断参数是否为空
        AssertUtil.hasLength(username,"用户名不能为空");
        AssertUtil.hasLength(password,"密码不能为空");
        UserInfo user = repository.findByPhone(username);
        if(user == null || !password.equals(user.getPassword())){
            throw new LogicException("账号或密码错误");
        }
        return user;
    }

    @Override
    public List<UserInfo> queryCity(String city) {
        return repository.findByCity(city);
    }
}
